﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using System.Windows.Media.Animation;

namespace MOJsdk
{
    /// <summary>
    /// Interaction logic for WindowControl.xaml
    /// </summary>
    public partial class WindowControl : UserControl
    {
        UserControl _control;
        WindowControlInfo _info;

        public WindowControl(UserControl Control, WindowControlInfo Info)
        {
            InitializeComponent();

            _control = Control;
            _info = Info;

            INIControl();
            INIEvents();
        }

        public string WindowTitle
        { 
            get { return _txt_header.Text; }
            set { _txt_header.Text = value; }
        }
        public void AddUserControl(UserControl Control)
        {
            _dock.Children.Clear();
            _dock.Children.Add(Control);
        }
        public UserControl GetUserControl()
        {
            if (_dock.Children.Count == 0)
                MojHost.GetHost.ReportError(new ErrorReporter()
                {
                    ErrorCode = "WCx0x1",
                    ErrorDetails = "Dock panel children equal '0'",
                    ErrorMessage = "Empty Children",
                });

            return _dock.Children[0] as UserControl;
        }

        public void INIControl()
        {
            WindowTitle = _info.WindowTitle;
            AddUserControl(_control);
            this.Width = _info.WindowWidth;
            this.Height = _info.WindowHeight;
        }
        public void INIEvents()
        {
            _btn_close.Click += ButtonClose;
            _btn_minimize.Click += ButtonMinimize;
        }

        private void ButtonClose(object sender, RoutedEventArgs e)
        {
            IHost theHost = MojHost.GetHost;
            theHost.HostCanvas.Children.Remove(this);
            OnClosed(this, new EventArgs());
        }
        private void ButtonMinimize(object sender, RoutedEventArgs e)
        {
            if (this.Height == _info.WindowHeight)
            {
                DoubleAnimation min = new DoubleAnimation(_info.WindowHeight, 20, new Duration(TimeSpan.FromMilliseconds(300)));
                this.BeginAnimation(FrameworkElement.HeightProperty, min);
            }
            else
            {
                DoubleAnimation max = new DoubleAnimation(20, _info.WindowHeight, new Duration(TimeSpan.FromMilliseconds(300)));
                this.BeginAnimation(FrameworkElement.HeightProperty, max);
            }
        }

        public void Close()
        {
            IHost theHost = MojHost.GetHost;
            theHost.HostCanvas.Children.Remove(this);
            OnClosed(this, new EventArgs());
        }

        public event EventHandler OnClosed;
        public Rectangle HeaderRectangle
        { get { return _rect_header; } }
    }

    public class WindowControlInfo
    {
        public WindowControlInfo(double width, double height)
        {
            WindowTitle = "WindowControl";
            WindowHeight = height;
            WindowWidth = width;
            DefaultPosition = WindowPosition.Center;
            CustomPosition = new CanvasElementPosition() { Left = 5, Top = 5, };
            ZIndex = 25;
        }

        public string WindowTitle { get; set; }
        public double WindowHeight { get; set; }
        public double WindowWidth { get; set; }
        public int ZIndex { get; set; }
        public WindowPosition DefaultPosition { get; set; }
        public CanvasElementPosition CustomPosition { get; set; }
        public CanvasElementPosition GetElementPosition()
        {
            double CHeight = MojHost.GetHost.HostCanvas.ActualHeight;
            double CWidth = MojHost.GetHost.HostCanvas.ActualWidth;

            switch (DefaultPosition)
            {
                case WindowPosition.Custom:
                    return CustomPosition;

                case WindowPosition.Center:
                    CanvasElementPosition pos = new CanvasElementPosition();
                    pos.Left = (CWidth / 2) - (WindowWidth / 2);
                    pos.Top = (CHeight / 2) - (WindowHeight / 2);
                    return pos;

                default:
                    return new CanvasElementPosition() { Left = 5, Top = 5 };
            }
        }
    }
    public class CanvasElementPosition
    {
        public double Left { get; set; }
        public double Top { get; set; }
    }
    public enum WindowPosition
    {
        Custom = -1,
        Center = 0,
    }
}
